/*
 * Copyright (c)e 2024-2024.Powered by neflibata
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.liuzian.marketserver.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liuzian.marketserver.dto.R;
import com.liuzian.marketserver.entity.User;
import com.liuzian.marketserver.mapper.UserMapper;
import com.liuzian.marketserver.service.ImageService;
import com.liuzian.marketserver.service.StoreService;
import com.liuzian.marketserver.service.UserService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

@Slf4j
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Lazy
    @Resource
    private ImageService imageService;

    @Lazy
    @Resource
    private StoreService storeService;

    @Override
    public R<User> queryUser(String phone) {
        log.info("get phone: {}", phone);
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getPhoneNumber, phone);
        User user = super.getOne(wrapper);
        log.info("check user info:{}", user == null ? "null" : user.toString());
        if (user == null) {
            return R.error("用户不存在");
        } else {
            user.setImage(imageService.getImageByUid(user.getUid()));
            user.setStores(storeService.getStoreByUid(user.getUid()));
            return R.success(user);
        }
    }

    @Override
    public R<String> updateUser(User user) {
        log.info("update user:{}", user);
        LambdaUpdateWrapper<User> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(User::getUid, user.getUid());
        wrapper.set(User::getUsername, user.getUsername());
        wrapper.set(User::getDescription, user.getDescription());
        boolean bool = super.update(wrapper);
        return bool ? R.success("用户信息更新成功") : R.error("更新失败");
    }

    @Override
    public R<String> Register(User user) {
        log.info("get form data {}", user);
        String newPassWord = DigestUtils.md5DigestAsHex(user.getPassword().getBytes());
        user.setPassword(newPassWord);
        super.save(user);
        return R.success("用户注册成功!");
    }
}
